<?php
namespace modules;
use \phiction\link;
use \phiction\array_utils as arr;

class page
{
    static function title(): string
    {
        return "CRM Lite";
    }

    static function home(): string
    {
        return "/welcome";
    }

    static function links(): array
    {
        return [
            "信息中心" => "/dashboard",
            "组织" => "/org",
            "用户" => [
                "在职用户" => "/user/active/1",
                "已离职用户" => "/user/archived/1",
            ],
            '联系人 ' => [
                '有效联系人' => '/contact/active/1',
                '已尘封联系人' => '/contact/archived/1',
            ],
            "商机" => [
                "有效商机" => "/potential/active/1",
                "已关闭商机" => "/potential/archived/1",
                "我负责的商机"=>"/potential/myself/1",
            ],
            "UI 展馆" => "/gallery",
        ];
    }

    static function skeleton($html, array $args): array
    {
        $title = $args['TITLE'];
        if (arr::get($args, 'SUBTITLE', $subtitle)) $title .= ' - ' . $subtitle;

        $body = $args['BODY'];
        if (!arr::get_or($args, 'BLANK_BODY', false)) {
            $body = [ 'div', 'class' => 'header-split',
                self::header($html, $args),
                [ 'div',
                    'class' => 'menu-split',
                    self::menu($html, $args),
                    [ 'div', 'class' => 'content-wrapper', $body ],
                ],
            ];
        }

        return $html->mark([ 'html',
            [ 'head',
                [ '/meta', 'charset' => 'utf-8' ],
                [ '/meta', 'name' => 'viewport', 'content' => 'width=device-width, initial-scale=1' ],
                [ 'title', $title ],
                [ '/link', 'rel' => 'icon', 'href' => '/static/favicon.png?v=nothing-yet' ],
                self::link_css("index.css"),
                self::link_css("font-awesome.min.css"),
                self::link_js("jquery-2.2.3.min.js"),
                self::link_js("index.js"),
            ],
            [ 'body', $body ],
        ]);
    }

    static function bad_page($html, array $args): array
    {
        $args['TITLE'] = "出错啦";
        unset($args['SUBTITLE']);
        $email = "support@crmlite.com";
        $args['BODY'] = $html->mark(['.',
            [ 'div', 'class' => 'error-title', "出错啦" ],
            [ 'p', "如有疑问，请联系技术支持： ", [ 'a', 'href' => "mailto:$email", [ 'tt', $email] ], " 。" ],
            [ 'pre', 'class' => 'block', $args['BODY'] ],
        ]);
        return self::skeleton($html, $args);
    }

    static function no_such_page($msg): string
    {
        return "404 找不到页面：$msg";
    }

    static function login(): string
    {
        return "/auth/login";
    }

    static function denied(): string
    {
        return "/auth/denied";
    }

    static function api_result_filter($result, array $args)
    {
        if (array_key_exists('token', $args)) {
            $secure = $args['HTTPS'];
            setcookie('token', $args['token'], 1 << 32, "/", "", $secure, true);
        }
        return $result;
    }

    static function page_result_filter($result, array $args)
    {
        if (array_key_exists('token', $args)) {
            $secure = $args['HTTPS'];
            setcookie('token', $args['token'], 1 << 32, "/", "", $secure, true);
        }
        return $result;
    }

    private static function link_css(string $css)
    {
        return [ '/link', 'rel' => 'stylesheet', 'href' => link::static("css/$css") ];
    }

    private static function link_js(string $js, bool $defer=false)
    {
        return [ 'script', 'src' => link::static("js/$js"), 'defer' => $defer ];
    }

    private static function header($html, array $args): array
    {
        $result = [
            'header',
            ['a', 'href' => '/', 'class' => 'site-name block', self::title()],
        ];
        if (arr::get($args, 'HEADER-CONTROL', $controls)) $result[] = $controls;
        return $html->mark($result);
    }

    private static function menu($html, array $args): array
    {
        $result = [
            'aside',
        ];
        $links = self::desugar_links(self::links());
        foreach ($links as $name => $link) {
            $result[] = [ 'a', 'href' => $link['link'], 'class' => 'main-link', $name ];
            foreach ($link['subs'] as $subname => $sublink)
                $result[] = [ 'a', 'href' => $sublink, 'class' => 'sub-link', $subname ];
        }
        return $html->mark($result);
    }

    // input:
    //      [
    //          "hello" => [
    //              "world" => "/world",
    //              "foo" => "/foo",
    //          ],
    //          "bar" => "/bar",
    //      ]
    //
    // output:
    //      [
    //          "hello" => [
    //              "link" => "/world",
    //              "subs" => [
    //                  "world" => "/world",
    //                  "foo" => "/foo",
    //              ],
    //          ],
    //          "bar" => [
    //              "link" => "/bar",
    //              "subs" => [],
    //          ]
    //      ]
    private static function desugar_links(array $links): array
    {
        foreach ($links as $name => &$link) {
            if (is_array($link)) {
                // get first element of $link, or "" if no element at all.
                $href = "";
                foreach ($link as $sublink) {
                    $href = $sublink;
                    break;
                }

                $link = [
                    'link' => $href,
                    'subs' => $link,
                ];
                continue;
            }

            if (is_string($link)) {
                $link = [
                    'link' => $link,
                    'subs' => [],
                ];
                continue;
            }

            throw new \InvalidArgumentException("links can only be string or array");
        }

        return $links;
    }
}

